package servlet;

import util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Author leiHua
 * @Date 2022/10/14 18:27
 */
@WebServlet("/deleteMajorServlet.do")
public class DeleteMajorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

        try {
            conn=JDBCUtils.getconnection();
            String sql="delete from tb_major where id=?";
            ps = conn.prepareStatement(sql);
            String id = request.getParameter("id");
            ps.setString(1,id);
            int i=ps.executeUpdate();
            if (i==1){
                request.getRequestDispatcher("/showAllMajors.do").forward(request,response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                JDBCUtils.close(rs,ps,conn);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
